/* GLCamera.cpp
 *
 * --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */

#include <graphics/GLCamera.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cmath>

GLCamera::GLCamera()
	: position(vector3d(0, 0, 0)), target(vector3d(0, 0, 0)), vertical(0, 1, 0),
	  theta(0), phi(0)
{
	updateLook();
}

void
GLCamera::updateCamera()
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(position[0], position[1], position[2],
			target[0], target[1], target[2],
			vertical[0], vertical[1], vertical[2]);
}

void
GLCamera::updateLook()
{
	double Rxz = std::cos(phi*M_PI/180);
	look[0] = Rxz*std::cos(theta*M_PI/180);
	look[2] = Rxz*std::sin(theta*M_PI/180);
	look[1] = std::sin(phi*M_PI/180);

	target = position+look;
}
